﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using UnityEngine;

namespace DashFire
{
  class ResSheetLoader
  {
    public static ResAsyncInfo LoadResSheet()
    {
      ResAsyncInfo info = new ResAsyncInfo();
      info.CurCoroutine = CoroutineProxy.Instance.StartCoroutine(LoadResSheetAsync(info));
      return info;
    }
    private static IEnumerator LoadResSheetAsync(ResAsyncInfo info)
    {
      if (ResUpdateControler.s_ClientVersionInfo.IsResInnerConfigCached) {
        info.IsDone = true;
        info.Progress = 1.0f;
        yield break;
      }
      string sheetZipFile = string.Format(ResUpdateConfig.s_ResSheetZipFormat, "");
      string url = ResLoadHelper.GetFilePath(ResPathType.Buildin, sheetZipFile);
      ResLoadHelper.Log("LoadResSheet url:" + url);
      AssetBundle assetBundle = null;
      if (!ResLoadHelper.IsResConfigCached(ResFileType.ResInner)) {
        using (WWW tWWW = new WWW(url)) {
          yield return tWWW;
          try {
            if (tWWW.error != null) {
              ResLoadHelper.Log("LoadResSheet www error url:" + url);
              tWWW.Dispose();
              ResUpdateControler.s_UpdateError = ResUpdateError.LoadResSheet_WWW_Error;
              yield break;
            }
            byte[] buffer = tWWW.bytes;
            if (buffer == null || buffer.Length < 0) {
              ResLoadHelper.Log("LoadResSheet tWWW.byte null");
              info.IsError = true;
              ResUpdateControler.s_UpdateError = ResUpdateError.LoadResSheet_Byte_Error;
              tWWW.Dispose();
              yield break;
            }
            ResUpdateTool.SaveResSheetAB(buffer, sheetZipFile, true);
          } catch (System.Exception ex) {
            ResLoadHelper.Log("LoadResSheet ab failed url:" + url + "ex:" + ex);
            info.IsError = true;
            ResUpdateControler.s_UpdateError = ResUpdateError.LoadResSheet_Save_Error;
            yield break;
          } finally {
            if (assetBundle != null) {
              assetBundle.Unload(true);
            }
            tWWW.Dispose();
          }
        }
      }

      info.IsDone = true;
      info.Progress = 1.0f;
    }
    public static ResAsyncInfo ExtractResSheet(string sheetZipFile, bool isHideLoading = false)
    {
      ResAsyncInfo info = new ResAsyncInfo();

      string resSheetZipFile = ResLoadHelper.GetFilePath(ResPathType.Cache, sheetZipFile);
      if (!File.Exists(resSheetZipFile)) {
        info.IsError = true;
        return info;
      }
      AssetBundle assetBundle = null;
      try {
        assetBundle = ResUpdateTool.LoadAssetBundle(resSheetZipFile);
        if (assetBundle == null) {
          ResLoadHelper.Log("LoadResSheet assetBundleObj null url:" + resSheetZipFile);
          ResUpdateControler.s_UpdateError = ResUpdateError.LoadResSheet_Assetbundle_Error;
          info.IsError = true;
          return info;
        }
        info.CurCoroutine = CoroutineProxy.Instance.StartCoroutine(ResUpdateTool.ExtractResSheet(info, assetBundle, isHideLoading));
      } catch (System.Exception ex) {
        ResLoadHelper.Log("LoadResSheet res failed url:" + resSheetZipFile + " ex:" + ex);
        ResUpdateControler.s_ClientVersionInfo.IsResInnerConfigCached = false;
        ResUpdateControler.s_ClientVersionInfo.Save();
        ResUpdateControler.s_UpdateError = ResUpdateError.LoadResSheet_Extract_Error;
        info.IsError = true;
      }
      return info;
    }
    public static void OnFinishExtractResSheetZip()
    {
      string resSheetZipFile = ResLoadHelper.GetFilePath(ResPathType.Cache, string.Format(ResUpdateConfig.s_ResSheetZipFormat, ""));
      try {
        if (File.Exists(resSheetZipFile)) {
          File.Delete(resSheetZipFile);
        }
      } catch (System.Exception ex) {
        ResLoadHelper.Log("LoadResSheet res failed url:" + resSheetZipFile + " ex:" + ex);
        return;
      }
    }
  }
}
